package com.example.service;

import com.example.entity.Chengji;
import com.example.mapper.ChengjiMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Service
public class ChengjiService {

    @Resource
    private ChengjiMapper chengjiMapper;

    public Chengji getRandomChengji(){
        List<Chengji> chengjis = chengjiMapper.selectAll(null);
        if (chengjis.isEmpty()){
            return null;
        }
        System.out.println(chengjis);
        Random random = new Random();
        return chengjis.get(random.nextInt(chengjis.size()));
    }

    public void add(Chengji chengji) {
        chengjiMapper.insert(chengji);
    }

    public void deleteById(Integer id) {
        chengjiMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            this.deleteById(id);
        }
    }

    public void updateById(Chengji chengji) {
        chengjiMapper.updateById(chengji);
    }

    public Chengji selectById(Integer id) {
        return chengjiMapper.selectById(id);
    }

    public List<Chengji> selectAll(Chengji chengji) {
        return chengjiMapper.selectAll(chengji);
    }

    public PageInfo<Chengji> selectPage(Chengji chengji, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Chengji> chengjiList = chengjiMapper.selectAll(chengji);
        return PageInfo.of(chengjiList);
    }

    public int selectCountById(String pingji) {
        return chengjiMapper.selectCountById(pingji);
    }
}
